
package com.timber.frame;


import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.timber.panels.AdminPanel;
import com.timber.panels.BillAdjustmentPanel;
import com.timber.panels.InvPanel;
import com.timber.panels.ItemMangerPanel;
import com.timber.panels.SalesPanel;
import com.timber.util.Global;
import com.timber.util.ImageMgr;
import com.timber.util.ImsFrameComponent;
import com.timber.util.SLDataChangeEvent;
import com.timber.util.SLImageButton;
import com.timber.util.SLMessage;
import com.timber.util.Util;
//import com.tim.panels.SalesPanel2;


class MyGlassPane extends JPanel implements KeyListener, MouseListener {
	public MyGlassPane() {
		setVisible(false);
		setOpaque(false);
		setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
	}

	public void keyPressed(KeyEvent ke) {
	}

	public void keyReleased(KeyEvent ke) {
	}

	public void keyTyped(KeyEvent ke) {
	}

	public void mouseClicked(MouseEvent me) {
	}

	public void mouseEntered(MouseEvent me) {
	}

	public void mouseExited(MouseEvent me) {
	}

	public void mousePressed(MouseEvent me) {
	}

	public void mouseReleased(MouseEvent me) {
	}
}

public class ImsFrame extends JFrame  {

	protected static ImsFrame mainFrame = null;

	protected JPanel contentPane;

	protected JMenuBar menuBar1 = new JMenuBar();

	protected JMenu menuFile = new JMenu();

	protected JMenuItem menuFileExit = new JMenuItem();

	protected JMenu menuApplications = new JMenu();

	protected JMenuItem menuApplicationsReturns = new JMenuItem();

	protected JMenuItem menuApplicationsShorts = new JMenuItem();

	protected JMenuItem menuApplicationsAutoBorrow = new JMenuItem();

	protected JMenuItem menuApplicationsAutoloan = new JMenuItem(); // DAW
																	// 24Feb2005

	protected JMenuItem menuApplicationsAutoReturns = new JMenuItem();

	// Added for Rehypo REfinancing opportunities
	protected JMenu menuApplicationsRehypoSuperMenu = new JMenu();

	// Added for Rehypo Returns
	protected JMenuItem menuApplicationsReHypoAutoReturns = new JMenuItem();

	// Added for rehypo Booking
	protected JMenuItem menuApplicationsReHypoBooking = new JMenuItem();
	
	protected JMenuItem menuApplicationsRehypoStreetCoverOppr = new JMenuItem();

	protected JMenuItem menuApplicationsRehypoShortCoverOppr = new JMenuItem();

	protected JMenuItem menuApplicationsCollMgmt = new JMenuItem();

	// New Collateral Management module
	protected JMenu menuApplicationsCollMgmtNew = new JMenu();

	protected JMenuItem menuApplicationsCollAvail = new JMenuItem();

	protected JMenuItem menuApplicationsCollCash = new JMenuItem();

	protected JMenuItem menuApplicationsCollUtilisation = new JMenuItem();

	protected JMenuItem menuApplicationsEscrowTripartyProjection = new JMenuItem();

	protected JMenuItem menuApplicationsCollateralProjection = new JMenuItem();

	// protected JMenuItem menuApplicationsAutoBorrowUS = new JMenuItem();
	protected JMenuItem menuApplicationsHERT = new JMenuItem();

	protected JMenuItem menuApplicationsCallbacks = new JMenuItem();

	protected JMenuItem menuApplicationsLoanetExceptions = new JMenuItem();

	protected JMenuItem menuApplicationsReturnsBlotter = new JMenuItem();

	// protected JMenuItem menuApplicationsPLReport = new JMenuItem();
	protected JMenuItem menuApplicationsTradeScreen = new JMenuItem();

	protected JMenuItem menuApplicationsReports = new JMenuItem();

	protected JMenuItem menuApplicationsExposure = new JMenuItem();

	protected JMenuItem menuApplicationsOSSM = new JMenuItem("OSSM");

	protected JMenuItem menuApplicationsIMS = new JMenuItem("IMS"); // DAW
																	// 1Sep2004

	protected JMenuItem menuApplicationsGlobalPrime = new JMenuItem(
			"Global Prime");

	protected JMenuItem menuApplicationsDbSlam = new JMenuItem("DBSLAM");

	protected JMenuItem menuApplicationsSecLending = new JMenuItem(
			"Securities Lending");

	protected JMenuItem menuApplicationsEquilend = new JMenuItem("Equilend");

	protected JMenuItem menuApplicationsExcessBox = new JMenuItem();

	protected JMenuItem menuApplicationsPbReturn = new JMenuItem();
	
	protected JMenuItem menuApplicationsPbOverborrow = new JMenuItem();

	protected JMenuItem menuApplicationsPbAvail = new JMenuItem();

	protected JMenuItem menuApplicationsPbAutoBorrowLoan = new JMenuItem();

	protected JMenuItem menuApplicationsSpecialLocates = new JMenuItem();

	protected JMenuItem menuApplicationsTradeApproval = new JMenuItem();

	protected JMenuItem menuApplicationsIntlCallbackMgr = new JMenuItem();

	protected JMenuItem menuApplicationsTriParty = new JMenuItem();
	
	protected JMenuItem menuApplicationsCorpBondHotSheet = new JMenuItem();

	protected JMenuItem menuApplicationsClientColour = new JMenuItem();

        //Exclusive admin
  	protected JMenuItem menuApplicationsExclAdmin = new JMenuItem();  


	protected JMenu menuUtilities = new JMenu();

	// protected JMenuItem menuUtilitiesCostCenter = new JMenuItem();
	protected JMenuItem menuUtilitiesInternalCparty = new JMenuItem();

	protected JMenuItem menutilitiesEscrowManager = new JMenuItem();

	protected JMenuItem menuUtilitiesCustomer = new JMenuItem();

	protected JMenuItem menuUtilitiesDesk = new JMenuItem();

	// ==**==
	protected JMenuItem menuUtilitiesSupplyPriority = new JMenuItem();

	protected JMenuItem menuUtilitiesDemandCustomerAccount = new JMenuItem();

	// ==**==
	protected JMenuItem menuUtilitiesUser = new JMenuItem();

	protected JMenuItem menuUtilitiesAddSec = new JMenuItem();

	protected JMenuItem menuUtilitiesLoadMon = new JMenuItem();

	protected JMenuItem menuUtilitiesRates = new JMenuItem();

	protected JMenuItem menuUtilitiesPBRates = new JMenuItem();

	protected JMenuItem menuUtilitiesNCCharge = new JMenuItem();

	protected JMenuItem menuUtilitiesGblCollRates = new JMenuItem();

	protected JMenuItem menuUtilitiesEquilend = new JMenuItem();

	protected JMenuItem menuUtilitiesAdmin = new JMenuItem("System Admin");

	protected JMenuItem menuUtilitiesChat = new JMenuItem("Chat");

	protected JMenuItem menuUtilitiesJMSMonitor = new JMenuItem("JMS Monitor");

	protected JMenuItem menuUtilitiesUserPreference = new JMenuItem();

	protected JMenuItem menuUtilitiesUserCustomerGroup = new JMenuItem(
			"User Customer Group");

	protected JMenuItem menuUtilitiesExclusive = new JMenuItem();

	protected JMenuItem menuUtilitiesPermissions = new JMenuItem();

	protected JMenuItem menuUtilitiesAllIn = new JMenuItem("All-In Manager");

	protected JMenu menuHelp = new JMenu();

	protected JMenuItem menuHelpAbout = new JMenuItem();

	protected JMenuItem menuHelpInfo = new JMenuItem();

	protected JMenuItem menuHelpInvDetail = new JMenuItem("Inventory Detail");

	protected JToolBar toolBar = new JToolBar();

	protected SLImageButton printButton = new SLImageButton("Print");

	private boolean rtUpdate = true;

	protected JLabel statusBar = new JLabel();

	protected BorderLayout borderLayout1 = new BorderLayout();

	protected JTabbedPane mainTabbedPane = new JTabbedPane();
	//protected InventoryPanel inventoryPanel;
	
	protected InvPanel inventoryPanel;
	
	protected SalesPanel salesPanel;
	
	protected AdminPanel adminPanel;
	
	
	protected ItemMangerPanel itemManagerPanel;
	protected BillAdjustmentPanel billAdjustmentPanel;
	
	public static final int INVDETAIL_PANEL = 1;

	protected JPanel pStatus = new JPanel();



	class SecurityIdStrings {
		public String securityCode = "";

		public String securityCodeType = "";
	}

	// Construct the frame
	public ImsFrame() {
		mainFrame = this;
		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		setGlassPane(new MyGlassPane());

		Global.setMainFrame(this);

		try {
			jbInit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Component initialization
	private void jbInit() throws Exception {
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

		
		String location ="LN"; //Global.getLocation();
		this.setSize(new Dimension(1065, 750));
		String title = "Excel";

		Global.setTitle(title);
		this.setTitle(title);

		//this.setIconImage(ImageMgr.getIcon(ImageMgr.IDELTA_ICON).getImage());

		contentPane = (JPanel) this.getContentPane();
		contentPane.setLayout(borderLayout1);

		setStatusText(" ");
		
		// cerating file menu option 
		
		menuFile.setMnemonic('F');
		menuFile.setText("File");
		menuFileExit.setMnemonic('x');
		menuFileExit.setText("Exit");
		menuFileExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fileExit_actionPerformed(e);
			}
		});
		menuFile.add(menuFileExit);
		menuBar1.add(menuFile);
		this.setJMenuBar(menuBar1);
		
		
		
		// tool bar 

		printButton.setMinimumSize(new Dimension(25, 25));
		printButton.setOpaque(false);
		printButton.setPreferredSize(new Dimension(70, 45));
		printButton.setMaximumSize(new Dimension(70, 50));
		printButton.setImageAt(ImageMgr.getIcon(ImageMgr.PRINT32_ICON)
				.getImage(), 0);
		printButton.setToolTipText("Print current table");
		printButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				doPrint(mainFrame);
			}
		});

		toolBar.setOrientation(JToolBar.VERTICAL);
		toolBar.setFloatable(false);
		toolBar.add(printButton);
		

		BorderLayout blStatus = new BorderLayout(0, 0);
		pStatus.setLayout(new BorderLayout(0, 0));

		pStatus.setPreferredSize(new Dimension(450, 20));

		contentPane.add(pStatus, BorderLayout.SOUTH);

		checkAndBuildMsgTickerStatus();

		setupAdminConnection();

		contentPane.add(toolBar, BorderLayout.WEST);
		contentPane.add(mainTabbedPane, BorderLayout.CENTER);

		mainTabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent ce) {
				setStatusText("");
			}
		});

		Dimension dim = new Dimension(400, 20);
		statusBar.setMinimumSize(dim);
		statusBar.setPreferredSize(dim);
		
		checkAndBuildMainTabs();
	}

	// File | Exit action performed
	public void fileExit_actionPerformed(ActionEvent e) {
		int ret = SLMessage.ConfirmYN(this, "Exit Application?");
		if (ret == SLMessage.YES)
			Global.exit();
	}





	// Applications Client Colour action performed
	public void menuApplicationsClientColour_actionPerformed(ActionEvent e) {}


	public void activateTab(int tabType, Object o) {
		

		switch (tabType) {
		case INVDETAIL_PANEL:
			if (inventoryPanel != null) {
				mainTabbedPane.setSelectedComponent(inventoryPanel);
				//inventoryPanel.activate(o);
				//inventoryPanel.setInitialFocus();
			}
			break;
		
		default:
			Util.error("ImsFrame.activateTab(): unknown tab type: " + tabType);
			break;
		}
		// tabbs
		
	}

	






	private void checkAndBuildToolBar() {}

	//TODO
	
	private void checkAndBuildMainTabs() {
		
			mainTabbedPane.removeAll();
			inventoryPanel = new InvPanel(this);
			mainTabbedPane.add(inventoryPanel, "Inventory");
			salesPanel=new SalesPanel(this);
			mainTabbedPane.add(salesPanel, "Slaes Dash Board ");
			itemManagerPanel=new ItemMangerPanel(this);
			mainTabbedPane.add(itemManagerPanel, "Item Manager");
			billAdjustmentPanel = new BillAdjustmentPanel(this);
			mainTabbedPane.add(billAdjustmentPanel, "Bill Adjustments");
			adminPanel = new AdminPanel(this);
			mainTabbedPane.add(adminPanel, "Admin");
			
	
	}

	private void checkAndBuildAppMenu() {}

	private void setupAdminConnection() {}

	/**
	 * called due to an admin message
	 */
	public void onAdminMessage(SLDataChangeEvent e) {}



	private void checkAndBuildMsgTickerStatus() {}
	
	public void setStatusText(String txt) {
		statusBar.setText(txt);
	}
	
	
	/**
	 * set focus to the first panel
	 */
//	public void setInitialFocus() {
//		try {
//			Component jc = mainTabbedPane.getComponentAt(0);
////			if (jc instanceof ImsFrameComponent)
////				((ImsFrameComponent) jc).setInitialFocus();
//		} catch (NullPointerException ex) {
//			SLMessage.Error(this, "Startup screen not set for the user...");
//			System.exit(0);
//		}
//	}
	
	
	protected void doPrint(JFrame parent) {
		mainFrame.getGlassPane().setVisible(true);

		ImsFrameComponent ifc = (ImsFrameComponent) mainTabbedPane
				.getSelectedComponent();
		if (ifc != null)
			ifc.printTable();
		else
			Util.error("ImsFrame.doPrint(): No currently selected tab...");

		mainFrame.getGlassPane().setVisible(false);
	}
	

	

}
